//
//  ActivityJoinView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/1.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ActivityJoinView.h"
#import "ActivityJoinTableViewCell.h"
#import "ActivityJoinDetailModel.h"
#import "LPRedViewAndLableView.h"
@interface ActivityJoinView ()<UITableViewDelegate,UITableViewDataSource,ActivityJoinCellDelegate,LPCostomHeadLineAndTextViewAndTiShiDelegate>
/**  tableVie  */
@property (strong, nonatomic) UITableView  *tableView;

/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

/**  活动数量  */
@property (strong, nonatomic) NSString  *actNumber;
/**  label  */
@property (strong, nonatomic) UILabel  *header;

/**  jc  */
@property (strong, nonatomic) JCAlertView  *alertView;
/**  弹窗的model  */
@property (strong, nonatomic) ActivityJoinDetailModel  *tishiModel;
/**  弹窗section  */
@property (assign, nonatomic) NSInteger  tishiSection;
/**  弹窗row  */
@property (assign, nonatomic) NSInteger  tishiRow;


@end
@implementation ActivityJoinView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        __weak typeof(self) weakSelf = self;
        [self.refurbish headerToScollview:self.tableView Block:^{
            weakSelf.page = 1;
            [weakSelf netWorking_header];
            
        }];
        [self.refurbish footerToScollview:self.tableView Block:^{
            weakSelf.page++;
            [weakSelf netWorking_footer];
        }];
        
        [self.tableView.mj_header beginRefreshing];
        [self layoutUI];
        [self addActNotifation];
    }
    return self;
}

- (void)addActNotifation{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actNotifation) name:@"acrRefresh" object:nil];
}

- (void)actNotifation{
    [self.tableView.mj_header beginRefreshing];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    
    NSString *GLOBALID2;
    if (ZJUSERINFORMODEL.DEPT_MODEL_JOB) {
        GLOBALID2 = ZJUSERINFORMODEL.DEPT_MODEL_JOB.GLOBALID;
    }else{
        GLOBALID2 = @"";
    }
   NSDictionary *paramer   = @{@"PAGE": page,
                  @"COUNT": @"10",
                               
                @"GLOBALID1": ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID ? ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID :@"",
                  @"GLOBALID2": GLOBALID2,
                  @"keyword": @"",
                  @"DEPTID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                  @"BIG_CLASS": @"",
                  @"SMALL_CLASS" : @"",
                  @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                  @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActListACT_NOT_OVER] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            self.header.text = [NSString stringWithFormat:@"共%@个活动",object[@"CNT"]];
            NSArray *array = [object objectForKey:@"ACT_NOT_OVER"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:array] mutableCopy] ;
            [weakSelf.tableView reloadData];
            
            
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSString *GLOBALID2;
    if (ZJUSERINFORMODEL.DEPT_MODEL_JOB) {
        GLOBALID2 = ZJUSERINFORMODEL.DEPT_MODEL_JOB.GLOBALID;
    }else{
        GLOBALID2 = @"";
    }
    NSDictionary *paramer   = @{@"PAGE": page,
                                @"COUNT": @"10",
                                @"GLOBALID1": ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID? ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID:@"",
                                @"GLOBALID2": GLOBALID2,
                                @"keyword": @"",
                                @"DEPTID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                                @"BIG_CLASS": @"",
                                @"SMALL_CLASS" : @"",
                                @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};

    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActListACT_NOT_OVER] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ACT_NOT_OVER"];
            if (array.count == 0) {
                [YJProgressHUD showMessage:@"没有数据" inView:[UIApplication sharedApplication].keyWindow];
                [weakSelf.tableView.mj_footer endRefreshing];
                return ;
                
            }
            
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:array]] ;
            [weakSelf.tableView reloadData];
            
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    self.header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 35 * [UIScreen zj_screenHScale])];
    self.header.backgroundColor = [UIColor themeGrayColor];
    self.header.textColor = [UIColor grayColor];
    self.header.font = LYPFONT(16);
    self.header.textAlignment = NSTextAlignmentCenter;
    self.tableView.tableHeaderView = self.header;
}



#pragma mark -ActivityJoinCellDelegate
- (void)ActivityJoinClick_index:(NSInteger)index section:(NSInteger)section statusText:(NSString *)statusText
{
    __weak typeof(self) weakSelf = self;
    ActivityJoinDetailModel *model = self.dataSouce[index];
    if ([statusText isEqualToString:@"加入"]) {
        [YJProgressHUD show:@"报名中..." inView:self mode:YJProgressModeLoading];
        /**  这是点击加入  */
        NSDictionary *paramer = @{@"AI_ID": model.ID,
                                  @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                  @"USER_NAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME};
        [NetWorkingManager postWithUrlString:[NetWorkingURL getactactionMobileEnroll] Parameter:paramer Success:^(id object) {
            if ([object[@"CODE"] isEqualToString:@"20000"]) {
                [MobClick event:[UMMStatisticsString registerAct]];
                [YJProgressHUD showMsgWithoutView:@"报名成功"];
                [weakSelf.tableView.mj_header beginRefreshing];
                
            }else if ([object[@"CODE"] isEqualToString:@"80001"]) {
                [YJProgressHUD showMsgWithoutView:@"重复报名"];
            }else if ([object[@"CODE"] isEqualToString:@"80002"]) {
                [YJProgressHUD showMsgWithoutView:@"活动结束"];
            }else if ([object[@"CODE"] isEqualToString:@"80003"]) {
                [YJProgressHUD showMsgWithoutView:@"人数达上限"];
            }else if ([object[@"CODE"] isEqualToString:@"80004"]) {
                [YJProgressHUD showMsgWithoutView:@"无此活动或已被删除!"];
            }else{
                [YJProgressHUD showMsgWithoutView:@"报名失败"];
            }
            
        } Fail:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"报名失败"];
        }];
    }else if([statusText isEqualToString:@"请假"]){
        
        LPCostomHeadLineAndTextViewAndTiShiView *tishi = [[LPCostomHeadLineAndTextViewAndTiShiView alloc] initWithFrame:CGRectMake(0, 0, self.width, [UIScreen zj_screenHeight]) headLine:@"请输入原因"];
        tishi.delegate = self;
        self.alertView = [[JCAlertView alloc] initWithCustomView:tishi dismissWhenTouchedBackground:YES];
        [self.alertView show];
        self.tishiModel = model;
        self.tishiSection = section;
        self.tishiRow = index;
        
    }
}

#pragma mark - LPCostomHeadLineAndTextViewAndTiShiDelegate
- (void)LPCostomHeadLineAndTextViewAndTiShiWithCancle:(BOOL)cancle{
    [self.alertView dismissWithCompletion:nil];
}

- (void)LPCostomHeadLineAndTextViewAndTiShiWithString:(NSString *)string{
    __weak typeof(self) weakSelf = self;
    /**   这里是请假  */
    [YJProgressHUD show:@"请假中..." inView:self mode:YJProgressModeLoading];
    NSDictionary *paramer = @{@"AI_ID": self.tishiModel.ID,
                              @"FROM_USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"FROM_USERNAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,
                              @"FROM_UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"PMID": ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID,
                              @"CONTENT": string,
                              @"TO_UNITID": self.tishiModel.UNITID,
                              @"FROM_UNITNAME": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.DEPT_NAME,
                              @"ACT_NAME": self.tishiModel.NAME};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActLeave] Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            [YJProgressHUD showMsgWithoutView:@"请假成功"];
            /**  请假成功修改数据源刷新界面  */
            weakSelf.tishiModel.IS_LEAVE = @"1";
            weakSelf.tishiModel.LEAVE_STATE = @"0";
            weakSelf.dataSouce[self.tishiRow] = weakSelf.tishiModel;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:weakSelf.tishiRow inSection:weakSelf.tishiSection];
            [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }else{
            [YJProgressHUD showMsgWithoutView:@"请假失败"];
        }
        
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"请假失败"];
    }];
    
    [self.alertView dismissWithCompletion:nil];
    
}


#pragma mark - /**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ActivityJoinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[ActivityJoinTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        
    }
    cell.delegate = self;
    cell.index = indexPath.row;
    cell.section = indexPath.section;
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSouce.count != 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"actModel" object:self userInfo:@{@"model": self.dataSouce[indexPath.row]}];
    }
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}


- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}



- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}


@end
